#include "my_client.h"
#include "ui_my_client.h"
#include <QHostAddress>

my_client::my_client(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::my_client)
{
    ui->setupUi(this);
    //1.申请内存
    this->myclient = new QTcpSocket(this);

    //2.关联连接成功信号
    connect(this->myclient, SIGNAL(connected()), this, SLOT(connect_success()));

    //3.关联数据接收信号
    connect(this->myclient, SIGNAL(readyRead()), this, SLOT(receive_data()));

    //4.连接服务器
    QString ip = "192.168.16.143";
    QString port = "1688";

    this->myclient->connectToHost(QHostAddress(ip),port.toUShort());
}

my_client::~my_client()
{
    delete ui;
}

//连接成功槽函数
void my_client::connect_success()
{
    qDebug()<<"连接成功";
    QString msg  = "你好，我是客户端";
    //发生数据
    if(!msg.isEmpty())
    this->myclient->write(msg.toUtf8());
}

//数据接收槽函数
void my_client::receive_data()
{
   QString msg  = this->myclient->readAll();
   qDebug()<<"客户端有数据可读, 数据是 "<<msg;
}
